<?php

/**

 * Created by PhpStorm.

 * User: win7

 * Date: 2018/5/18 0018

 * Time: 9:25

 */



namespace XtwsBack\Controller;

use Think\Controller;



class FarmersController extends Controller

{

    //显示农户信息

    public function index(){

        $member = M('member');

        $count = $member->where('u_type = 2 ')->count();// 查询满足要求的总记录数

        $Page = new \Think\Page($count,100);// 实例化分页类 传入总记录数和每页显示的记录数(25)

        $show = $Page->show();// 分页显示输出

		$where="";
		if($_GET['keyword']!=''){
			$where=" and nickname like '%".$_GET['keyword']."%'";
			$this->assign('keyword',$_GET['keyword']);// 赋值分页输出
		}

        $member_arr =$member->where('u_type = 2 '.$where)->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select();



        foreach($member_arr as $k => $v){

            $farmers = M("member_farmers")->where("u_id = {$v['id']}")->find();

            $show_info[] = array(

                'id' => $farmers['id'],

                'u_id' => $v['id'],

                'nickname' => $v['nickname'],

                'realname' => $v['realname'],

                'address' => getarea($farmers['a_id_1']).getarea($farmers['a_id_2']).getarea($farmers['a_id_3']).getarea($farmers['a_id_4']).getarea($farmers['a_id_5']).$farmers['village']

            );

        }

        $this->assign('list',$show_info);// 赋值数据集

        $this->assign('page',$show);// 赋值分页输出

        $this->display();

    }//end fun index()



    //    添加农户

    public function addfarmers(){

        $list_member = M("member")->where('u_type = 0')->field('id,nickname')->select();//获取用户列表

        $getfirst=M('area')->where("`re_id` =0")->select();//获取一级所有地址



        if(IS_POST){

            $addfarmers = M('member_farmers')->add($_POST);

            $savemember = M("member")->where("id = {$_POST['u_id']}")->save(['u_type'=> 2]);

            if ($addfarmers && $savemember){

                $this->success('添加成功！','index.html');

                return;

            }else{

                $this->error('添加失败');

            }

        }//end if post



        $this->assign('areali',$getfirst);

        $this->assign('list_member',$list_member);

        $this->display();

    }//end fun add



    //编辑农户

    public function savefarmers(){

        $id = I('get.id')?I('get.id'):I('post.id');

        $farmers = M('member_farmers')->where("id = {$id}")->find();//获取农户信息

        $list_member = M("member")->field('id,nickname')->select();//获取用户列表

        $getfirst=M('area')->where("`re_id` =0")->select();//获取一级所有地址



        $post_farmers = $_POST;

        if(IS_POST){

            foreach($post_farmers as $k => $v){

                //过滤未更改项

                if ($post_farmers[$k] == $farmers[$k]){

                    unset($post_farmers[$k]);

                }

                //过滤默认为零的一级地址

                if($k == 'a_id_1' && $v === "0"){

                    unset($post_farmers[$k]);

                }

            }

            if ($post_farmers){

                $savefarmers = M('member_farmers')->where("id = {$id}")->save($post_farmers);

                if ($savefarmers){

                    $this->success('编辑成功',"index.html");

                    return;

                }else{

                    $this->error('编辑失败');

                }

            }else{

                $this->error('无修改');

            }

        }

        $this->assign('areali',$getfirst);

        $this->assign('list_member',$list_member);

        $this->assign($farmers);

        $this->display();

    }//end fun save



    public function deletefarmers(){

        $id = I('get.id')?I('get.id'):I('post.id');

        $u_id = M('member_farmers')->where("id = {$id}")->getField('u_id');//获取农户信息

        if ($id){

            $unsetfarmers = M('member_farmers')->where("id = {$id}")->delete();

            $savemember = M('member')->where("id = {$u_id}")->save(['u_type'=>0]);

            if ($unsetfarmers && $savemember){

                $this->success('删除成功');

                return;

            }else{

                $this->error('删除失败');

            }

        }

    }//end fun delete

}//end class